﻿<?php

/**
 * Lop tao form đăng nhập và đăng ký tài khoản với website
 */
 
class SiteController extends Controller
{

	public $layout='//layouts/column2';

	
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be modelessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	/*public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view_job'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','post'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionViewjob($id)
	{
		$this->render('viewjob',array(
			'model'=>$this->loadModel($id),
		));
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Job::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	/**
	 * Display Rigister page
	 */
	public function actionRigister()
	{
		$model = new LoginForm();
		if(isset($_POST['LoginForm']))
		{
			$model->attributes = $_POST['LoginForm'];
			if($model->validate())
			{
				if($model->save())
				{
					Yii::app()->user->setFlash("success",'Cám Ơn Bạn Đã Đăng Ký!');
					//$this->redirect(Yii::app()->user->returnUrl);
				}
			}
		}
		$this->render('rigister',array("model"=>$model));
	}
	/**
	 * Display login page
	 */
	public function actionLogin()
	{
		$model = new LoginForm();
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		
		if(isset($_POST['LoginForm']) || (isset($_POST['username']) && isset($_POST['password'])))
		{
			if ((isset($_POST['username']) && isset($_POST['password'])))
			{
				$username_re = $_POST['username'];
				$password_re = $_POST['password'];
				if($model->login($username_re, $password_re))
					$this->redirect(Yii::app()->user->returnUrl);
			}
			
			if(isset($_POST['LoginForm']))
			{
				$model->attributes=$_POST['LoginForm'];
				// validate user input and redirect to the previous page if valid
				if(!$model->validate() && $model->login($_POST['LoginForm']['username'],$_POST['LoginForm']['password']))
					$this->redirect(Yii::app()->user->returnUrl);
			}
		}
		$this->render('Login',array("model"=>$model));
		
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		if (isset($_GET['search']))
		{
			$list = new Job();
			$dataProvider = $list->searchIndex($_GET['search']);
			$this->render('index',array(
			'dataProvider'=>$dataProvider,
			));
		}
		else
		{
			$dataProvider=new CActiveDataProvider('Job');
			$this->render('index',array(
				'dataProvider'=>$dataProvider,
			));
			//$model = new Job();
			//$dataProvider=Job::model()->findAll();
			//$this->render('index',array('dataProvider'=>$dataProvider));
		}
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}
	
	/**
	 * Display logout page
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}